<?php
namespace app\common\tool\profit_sharing;

class Contributing extends Base {
	public function share($order, $receivers) {
		$app = loadTool('Wechat')->payment($order['route']);
		$resultValidate = loadValidate('wechat\\EasyWchatResult');
	    foreach ($receivers as $k => $v) {
	        $receiver = [
	            "type"          => $v['type'],
	            "account"       => $v['receiver_account'],//MERCHANT_ID：商户ID
	            "name"          => "偶像缘",//商户全称
	            "relation_type" => "HEADQUARTER"
	        ];
	        $result = $app->profit_sharing->addReceiver($receiver);
	        if(!$resultValidate->check($result)) {
	        	throw new \Exception($resultValidate->getError());
	        }
	        $receivers[$k]['account'] = $receivers[$k]['receiver_account'];
	        unset($receivers[$k]['receiver_account']);
	    }  
	    
	    $result = $app->profit_sharing->share($order['transaction_id'], $order['out_trade_no'], $receivers);
	    if(!$resultValidate->check($result)) {
        	throw new \Exception($resultValidate->getError());
        }
	    return $result;
	}
}
